<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<?php
if($_GET['mail']=='send')
{

	$sql="select Email from administration";
	$row=mysql_query($sql);
	$data=mysql_fetch_array($row);
	$from=$data['Email'];
	$to=$_POST['to'];
	$subject=$_POST['subject'];
    $file_name = "";
	$file_content = "";
		if($_FILES['attach']['name']!="")
		{
			$file_content = $_FILES['attach']['tmp_name'];
			$file_content = file_get_contents($file_content);
			$file_name = $_FILES['attach']['name'];
		}
   	$email_message = $_POST['maincont'];
	$fdata = chunk_split(base64_encode($file_content)); //Encode data into text form
			//Determine mime type
			$ext = explode('.', $file_name);
			$ext = $ext[1];
			
			$mime_type = "text/html";	
			$headers = "From:<".$from.">";
			
			$semi_rand = md5(time());
			$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
			
			$headers .= "\nMIME-Version: 1.0\n" .
			"Content-Type: multipart/mixed;\n" .
			" boundary=\"{$mime_boundary}\"";
			
			$email_message .= "This is a multi-part message in MIME format.\n\n" .
			"--{$mime_boundary}\n" .
			"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
			"Content-Transfer-Encoding: 7bit\n\n" .
			$email_message . "\n\n";
			
			$fileatt_type = "text/html"; // File Type
			
			$email_message .= "--{$mime_boundary}\n" .
			"-: {$fileatt_type};\n" .
			" name=\"{$file_name}\"\n" .
			"Content-Disposition: attachment;\n" .
			" filename=\"{$file_name}\"\n" .
			"Content-Transfer-Encoding: base64\n\n" .
			$fdata . "\n\n" .
			"--{$mime_boundary}\n";		
	 mail($to,$subject,$email_message,$headers);
 $ssql="update contact set mail_status='1' where id ='".$_GET['_d']."'";
	
	 mysql_query($ssql);
	 echo "<script>window.location='index.php?tc=contact&_sus=1'</script>";
}
$id = $_REQUEST['_d'];
$page = $_REQUEST['page'];

if($_REQUEST['_a'] != "u" && $_REQUEST['_a'] != "d")
{
$q = mysql_fetch_array(mysql_query("select * from contact where id='$id'"));

?>



<table width="99%" align="center" border="0"  class="list" cellpadding="5">
<thead>
<tr>
	<td colspan="2" align="left">
        <div style="float:left">Details</div>
        <div style="float:right"><a href="index.php?tc=contact" class="font12">Back</a></div>
    </td>
</tr>
</thead>
<tr class="font12">
	<td width="207" align="right" valign="middle">First Name</td>
    <td width="883" align="left" valign="middle" style="padding-left:10px;"><?php echo $q['name'];?></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">Company</td>
    <td align="left" valign="middle" style="padding-left:10px;"><?php echo $q['company'];?></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">E-mail</td>
    <td align="left" valign="middle" style="padding-left:10px;"><?php echo $q['email'];?><div style="float:right">
    <a href="index.php?tc=contact_view&page=reply&_d=<?php echo $q['id'];?>">Click here to reply</a></div></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">Address</td>
    <td align="left" valign="middle" style="padding-left:10px;"><?php echo $q['street_address'];?></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">City</td>
    <td align="left" valign="middle" style="padding-left:10px;"><?php echo $q['city'];?></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">State</td>
    <td align="left" valign="middle" style="padding-left:10px;"><?php echo $q['state'];?></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">Country</td>
    <td align="left" valign="middle" style="padding-left:10px;"><?php echo $q['country'];?></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">Zip</td>
    <td align="left" valign="middle" style="padding-left:10px;"><?php echo $q["zip_code"];?></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">Contact</td>
    <td align="left" valign="middle" style="padding-left:10px;"><?php echo $q['phone'];?></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">Fax</td>
    <td align="left" valign="middle" style="padding-left:10px;"><?php echo $q['fax'];?></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">Comments</td>
    <td align="left" valign="middle" style="padding-left:10px;"><?php echo $q['comments'];?></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">Created Date</td>
    <td align="left" valign="middle" style="padding-left:10px;">
	<?php 
	 $dateArray=explode('-',$q['created_date']);
	echo date('M j, Y', mktime(0, 0, 0, $dateArray[1], $dateArray[2], $dateArray[0])); 
	
	?></td>
</tr> 
 
<tr>
	<td colspan="2" style="padding-left:155px;">&nbsp;</td>
</tr>        
</table>   

<?php
$action='index.php?tc=contact_view&mail=send&page=reply&_d='.$_GET['_d'];

if($_GET['page']=='reply')
{
?>
<form name="mail" action="<?php echo $action;?>" method="post" enctype="multipart/form-data">
<table width="99%" align="center" border="0"  class="list" cellpadding="5">
<thead>
<tr>
	<td colspan="2" align="left">
        <div style="float:left">Mail</div>
       
    </td>
</tr>
</thead>
<tr class="font12">
	<td width="207" align="right" valign="middle">TO</td>
    <td width="883" align="left" valign="middle" style="padding-left:10px;"><input name="to" type="text" readonly="readonly"
    value="<?php echo $q['email'];?>" style="border:#F9F9F9 solid 1px; background-color:#F9F9F9; width:100%; color:#006699;" /></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">Subject</td>
    <td width="883" align="left" valign="middle" style="padding-left:10px;"><input type="text" name="subject" style="width:100%;" /></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="top">Message</td>
  
    <td align="left" colspan="100%">
    <textarea name="maincont" id="maincont" class="textarea" cols="45" rows="8"></textarea>
        <script type="text/javascript">CKEDITOR.replace( 'maincont',{skin : 'kama'});</script>
    </td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle">Attachment</td>
    <td width="883" align="left" valign="middle" style="padding-left:10px;"><input type="file" name="attach" /></td>
</tr>
<tr class="font12">
	<td width="207" align="right" valign="middle"></td>
    <td width="883" align="left" valign="middle" style="padding-left:10px;"><input type="submit" name="send_mail" value="send"  />
    <input type="submit" value="Discard"  />
    </td>
</tr>
</table>
</form>
<?php } }?>